using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for DbProvider
/// </summary>
public class DbProvider
{
    public DbProvider() { }

    /// <summary>
    /// Get connection string stored in Web.config
    /// </summary>
    public static string ConnectionString
    {
        get
        {
            return ConfigurationManager.ConnectionStrings["AuctionCnStr"].ConnectionString;
        }
    }

    /// <summary>
    /// Create a SqlCommand based on Store Procedure Name
    /// </summary>
    public static SqlCommand CreateCommand(string procName)
    {
        SqlConnection _Cn = new SqlConnection(ConnectionString);
        SqlCommand cmd = new SqlCommand(procName, _Cn);
        cmd.CommandType = CommandType.StoredProcedure;
        return cmd;
    }

    /// <summary>
    /// Execute SqlCommand
    /// </summary>
    public static void ExecCmd(SqlCommand cmd)
    {
        try
        {
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            cmd.Connection.Close();
        }
        catch { }
    }

    /// <summary>
    /// Load a dataset from Store Procedure
    /// </summary>
    public static DataSet LoadData(string procName)
    {
        try
        {
            SqlConnection _Cn = new SqlConnection(ConnectionString);
            SqlDataAdapter da = new SqlDataAdapter(procName, _Cn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }
        catch { return null; }
    }

    /// <summary>
    /// Load a dataset from Store Procedure, procedure has some parameters
    /// </summary>
    public static DataSet LoadData(string procName, params SqlParameter[] Params)
    {
        try
        {
            SqlConnection _Cn = new SqlConnection(ConnectionString);
            SqlDataAdapter da = new SqlDataAdapter(procName, _Cn);
            SqlCommand cmd = da.SelectCommand;
            cmd.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter param in Params) cmd.Parameters.Add(param);

            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }
        catch { return null; }
    }
}
